<!DOCTYPE html>
<html lang="zh">

<head>

    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>页面</title>
    <script src="https://pinzafile.oss-cn-shanghai.aliyuncs.com/vue-element/loading.js"></script>
    <link href="https://pinzafile.oss-cn-shanghai.aliyuncs.com/vue-element/lib/theme-chalk/index.css" rel="stylesheet">
    <link href="https://pinzafile.oss-cn-shanghai.aliyuncs.com/vue-element/treeTable.css" rel="stylesheet">


</head>

<body>
<div id="app">
    <el-container>
        <el-header>
            <el-breadcrumb separator-class="el-icon-arrow-right">
                <el-breadcrumb-item>权限管理</el-breadcrumb-item>
                <el-breadcrumb-item>权限列表</el-breadcrumb-item>
            </el-breadcrumb>
        </el-header>
        <el-main>
            <div class="block">
                <el-form label-width="80px" label-position="top">
                    <tree-table :data="dataTree" :eval-func="func" :eval-args="args" border size="mini"
                                height="calc(100vh - 200px)"
                                style="margin-left:1%;width:98%;overflow:auto">
                        <el-table-column label="地址" align="center">
                            <template slot-scope="scope">
                                <span style="color:#606266;font-size: 14px"></i>{{
                                scope.row.address}}</span>
                            </template>
                        </el-table-column>
                        <el-table-column label="操作" align="center">
                            <template slot-scope="scope">
                                        <#if add == "success">
                                            <el-button type="text" size="mini" @click="append(scope.row)">添加</el-button>
                                        </#if>
                                            <#if update == "success">
                                            <el-button type="text" size="mini" @click="modify(scope.row)">编辑</el-button>
                                            </#if>
                            </template>
                        </el-table-column>
                    </tree-table>
                <#--<el-tree :data="dataTree" show-checkbox node-key="id" default-expand-all-->
                <#--:expand-on-click-node="false">-->
                <#--<span class="custom-tree-node" slot-scope="{ node, data }">-->
                <#--<span>{{ node.label }} ：{{data.address}}</span>-->
                <#--<span>-->
                <#--<#if add = "success">-->
                <#--<el-button type="text" size="mini" @click="() => append(data)">-->
                <#--添加-->
                <#--</el-button>-->
                <#--</#if>-->
                <#--<#if update = "success">-->
                <#--<el-button type="text" size="mini" @click="() => modify(node,data)">-->
                <#--编辑-->
                <#--</el-button>-->
                <#--</#if>-->
                <#--<el-button type="text" size="mini" @click="() => remove(node, data)">-->
                <#--删除-->
                <#--</el-button>-->
                <#--</span>-->
                <#--</span>-->
                <#--</el-tree>-->
                </el-form>
            </div>
        </el-main>
        <!--新增资源模态框 -->
        <el-dialog title="请输入资源信息" :visible.sync="dialogAppendVisible">
            <el-form>
                <el-form-item label="资源名称">
                    <el-input v-model="label" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="资源地址">
                    <el-input v-model="address" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogAppendVisible = false">取 消</el-button>
                <el-button type="primary" @click="registerLogin">确 定</el-button>
            </div>
        </el-dialog>
        <!-- 修改资源模态框 -->
        <el-dialog title="请确认资源信息" :visible.sync="dialogModifyVisible">
            <el-form>
                <el-form-item label="资源名称">
                    <el-input v-model="label" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="资源地址">
                    <el-input v-model="address" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogModifyVisible = false">取 消</el-button>
                <el-button type="primary" @click="newly">确 定</el-button>
            </div>
        </el-dialog>
        <el-footer>

        </el-footer>
    </el-container>
</div>

<script src="https://pinzafile.oss-cn-shanghai.aliyuncs.com/vue-element/dist/vue.js"></script>
<script src="https://pinzafile.oss-cn-shanghai.aliyuncs.com/vue-element/lib/index.js"></script>
<script src="https://pinzafile.oss-cn-shanghai.aliyuncs.com/vue-element/treeTable.js"></script>
<script src="https://pinzafile.oss-cn-shanghai.aliyuncs.com/publiccss/assets/global/plugins/jquery.min.js"
        type="text/javascript"></script>
<script>
    new Vue({
        el: '#app',
        data() {
            return {
                id: '',
                label: '',
                address: '',
                dialogAppendVisible: false,
                dialogModifyVisible: false,
                dataTree: ${list},
                data: '',
                tempNode: {},
            }
        },
        methods: {
            //添加节点  打开弹窗 清空缓存的节点信息，并缓存当前添加的父节点信息
            append(data) {
                this.label = '';
                this.address = '';
                this.dialogAppendVisible = true;
                this.tempNode = data;
            },
            //添加节点信息，发送请求
            registerLogin() {
                const newChild = {
                    //ID赋值
                    // id: id++,
                    parent_id: this.tempNode.id,
                    //名称
                    permission_name: this.label,
                    permission_url: this.address,
                    //子节点默认空
                    children: []
                };
                const addChild = {
                    //ID赋值
                    // id: id++,
                    id: this.tempNode.id,
                    //名称
                    label: this.label,
                    address: this.address,
                    //子节点默认空
                    children: []
                };
                //发送请求
                var bool = true;
                var dataId = 0;
                $.ajax({
                    url: '/permission/operaPermission.json',
                    type: 'post',
                    data: newChild,
                    async: false,
                    dataType: 'json',
                    success: function (result) {
                        bool = result.ret;
                        dataId = result.data;
                    }
                });
                if (bool) {
                    if (!this.tempNode.children) {
                        this.$set(this.tempNode, 'children', []);
                    }
                    //存值
                    addChild.id = dataId;
                    this.tempNode.children.push(addChild);
                    // 关闭弹窗
                    this.dialogAppendVisible = false;
                    this.$message({
                        type: 'success',
                        message: '添加成功!'
                    });
                } else {
                    this.$message({
                        type: 'success',
                        message: '数据添加异常!'
                    });
                }
            },
            //打开弹窗 获取当前修改的节点信息放入缓存
            modify(data) {
                console.log(data);
                this.dialogModifyVisible = true;
                //当前修改节点ID
                this.id = data.id;
                //当前修改参数1，重新赋值
                this.label = data.label;
                //当前修改参数2，重新赋值
                this.address = data.address
                this.tempNode = data;
            },
            //提交当前修改的节点信息
            newly() {
                //发送请求
                var updateBool = false;
                $.ajax({
                    url: '/permission/operaPermission.json',
                    type: 'post',
                    data: {id: this.id, permission_name: this.label, permission_url: this.address},
                    async: false,
                    dataType: 'json',
                    success: function (result) {
                        updateBool = result.ret;
                    }
                });
                if (updateBool) {
                    this.tempNode.label = this.label;
                    this.tempNode.id = this.id;
                    this.tempNode.address = this.address;
                    //获取返回值，替换dataTree
                    //关闭弹窗
                    this.dialogModifyVisible = false;
                } else {
                    this.$message({
                        type: 'success',
                        message: '数据修改异常!'
                    });
                }
            },
            //删除当前节点信息
            remove(data) {
                this.$confirm('此操作将永久删除该资源, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                    center: true
                }).then(() => {
                    var bool = false;
                    var msg = '';
                    $.ajax({
                        url: '/role/del.json',
                        type: 'get',
                        data: {id: data.id},
                        async: false,
                        dataType: 'json',
                        success: function (result) {
                            if (result.ret) {
                                bool = result.ret;
                            } else {
                                msg = result.msg;
                            }
                            ;
                        }
                    });
                    if (bool) {
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                        this.ajaxFunction();
                    } else {
                        this.$message.error(msg);
                    }
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });

            },

        },

    })
</script>
</body>

</html>